package com.bichengcao.googlecodejam.qualification2008;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;

public class FlySwatter {

	static int N;

	public static void main(String[] args) throws IOException {
		Input[] inputs = parseInput("src/com/bichengcao/googlecodejam/qualification2008/C-large-practice.in");
		Output[] outputs = process(inputs);
		writeOutput("src/com/bichengcao/googlecodejam/qualification2008/FlySwatter.out", outputs);
	}

	static class Input {

		double f, R, t, r, g;
	}

	static class Output {

		double P;
	}

	private static Input[] parseInput(String filename) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(filename));
		N = Integer.parseInt(in.readLine());
		Input[] array = new Input[N];

		for (int i = 0; i < N; i++) {
			Input input = array[i] = new Input();
			String line = in.readLine();
			String[] splits = line.split(" ");
			input.f = Double.parseDouble(splits[0]);
			input.R = Double.parseDouble(splits[1]);
			input.t = Double.parseDouble(splits[2]);
			input.r = Double.parseDouble(splits[3]);
			input.g = Double.parseDouble(splits[4]);
		}

		in.close();
		return array;
	}

	private static Output[] process(Input[] inputs) {
		Output[] outputs = new Output[N];
		for (int i = 0; i < N; i++) {
			outputs[i] = process(inputs[i]);
		}
		return outputs;
	}

	private static Output process(Input input) {
		Output output = new Output();

		if (input.f * 2 >= input.g) {
			output.P = 1.0;
			return output;
		}
		input.g -= input.f * 2;
		input.t += input.f;
		input.r += input.f;

		double x1 = input.r;
		double xBoundary = Math.sqrt(((input.R - input.t) * (input.R - input.t)) - input.r
				* input.r);
		while (x1 < xBoundary) {
			double x2 = x1 + input.g;
			double num = 0;
			if (input.R - input.t > x2) {
				num = Math
						.floor((Math.sqrt((input.R - input.t) * (input.R - input.t) - (x2 * x2)) + input.r)
								/ (input.g + 2 * input.r));
				output.P += num * input.g * input.g;
			}
			double y1 = num * (input.g + 2 * input.r) + input.r;
			while (y1 < Math.sqrt((input.R - input.t) * (input.R - input.t) - x1 * x1)) {
				output.P += calculateArea(x1, y1, input);
				y1 += input.g + 2 * input.r;
			}
			x1 += input.g + 2 * input.r;
		}

		output.P = 1 - output.P * 4 / (Math.PI * input.R * input.R);

		return output;
	}

	/**
	 * Calculate area for irregular holes.
	 */
	private static double calculateArea(double x1, double y1, Input input) {
		double R = input.R - input.t;
		double x2 = x1 + input.g;
		double y2 = y1 + input.g;
		double maxy2 = Math.sqrt(R * R - x1 * x1);
		double maxx2 = Math.sqrt(R * R - y1 * y1);
		double result = 0;
		if (x2 < maxx2 && y2 < maxy2) {
			double x3 = Math.sqrt(R * R - y2 * y2);
			double y3 = Math.sqrt(R * R - x2 * x2);
			result = input.g * input.g - calSubArea(x3, y2, x2, y3, R);
		} else if (x2 < maxx2 && y2 >= maxy2) {
			double y3 = Math.sqrt(R * R - x2 * x2);
			result = (maxy2 - y1) * input.g - calSubArea(x1, maxy2, x2, y3, R);
		} else if (x2 >= maxx2 && y2 < maxy2) {
			double x3 = Math.sqrt(R * R - y2 * y2);
			result = (maxx2 - x1) * input.g - calSubArea(x3, y2, maxx2, y1, R);
		} else if (x2 >= maxx2 && y2 >= maxy2) {
			result = (maxx2 - x1) * (maxy2 - y1) - calSubArea(x1, maxy2, maxx2, y1, R);
		}
		return result;
	}

	private static double calSubArea(double x1, double y1, double x2, double y2, double R) {
		double angle1 = Math.atan(x1 / y1);
		double angle2 = Math.atan(y2 / x2);
		double angle = Math.PI / 2 - angle1 - angle2;
		return (x2 * y1 - (angle * R * R + y1 * x1 + y2 * x2) / 2);
	}

	private static void writeOutput(String filename, Output[] outputs) throws IOException {
		PrintWriter out = new PrintWriter(new FileWriter(filename));
		DecimalFormat df = new DecimalFormat("0.000000");
		for (int i = 0; i < N; i++) {
			out.println("Case #" + (i + 1) + ": " + df.format(outputs[i].P));
		}
		out.close();
	}
}
